package cn.donting.jme.edit.attribute;

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;

import java.math.BigDecimal;

public class SliderAttribute extends Attribute<Float> {

    private HBox hBox = new HBox();
    private Slider slider = new Slider();
    private Label value = new Label();

    public SliderAttribute(String title) {
        super(title);
        slider.valueProperty().addListener((observable, oldValue, newValue) -> {
            BigDecimal bg = new BigDecimal(newValue.doubleValue());
            float vlu = bg.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            value.setText(vlu + "");
            setSuperValue(vlu);
        });
        hBox.getChildren().add(slider);
        hBox.getChildren().add(value);
        value.setText(slider.getValue() + "");
        slider.setBlockIncrement(0.1);
    }

    public SliderAttribute() {
        this("");
    }

    @Override
    public void setValue(Float value) {
        slider.setValue(value);
    }

    public void setMax(Float value) {
        slider.setMax(value);
    }

    public void setMin(Float value) {
        slider.setMin(value);
    }


    private void setSuperValue(Float value) {
        super.setValue(value);
    }

    @Override
    public Node getUINode() {
        return hBox;
    }

}
